package com.gjy.design.responsibility.r1;

import java.util.function.Function;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-11-11 11:10:17
 */
public class LambdaChainOfResponsibility {

    public static void main(String[] args) {
        // 定义处理者作为UnaryOperator
        Function<String, String> handlerA = request -> {
            if (request.equals("A")) {
                return "HandlerA 处理了: " + request;
            } else {
                return request; // 传递给下一个
            }
        };

        Function<String, String> handlerB = request -> {
            if (request.equals("B")) {
                return "HandlerB 处理了: " + request;
            } else {
                return request;
            }
        };

        // 构建链：通过链式组合
        Function<String, String> chain = handlerA.andThen(handlerB);

        // 使用链处理请求
        String resultA = chain.apply("A"); // 输出: HandlerA 处理了: A
        String resultB = chain.apply("B"); // 输出: HandlerB 处理了: B
        String resultC = chain.apply("C"); // 输出: C（无处理者处理）

        System.out.println(resultA);
        System.out.println(resultB);
        System.out.println(resultC);
    }


}
